<?php
  /*
   * Author : 1. Novi Setiani
   *          2. Gisca
   * Last edited : 4 Mei 2011
 */
    class Pengguna extends CI_Model
    {
        function Pengguna_model() {
                parent::Model();
        }

        //Cari nama penguna berdasar keyword nama
        function cari($q, $activity_id=null){
           $sql = "select userID, name from pengguna where name like '%$q%' ";
           /*  if ($activity_type == 1)
                                    $peserta = 'peserta';
                    else
                                    $peserta = 'pesertanonrutin';*/
            if($activity_id){
                $sql .= "AND userID not in (select userID from peserta where activityID = $activity_id)";
            }
            //echo $sql;
            $query = $this->db->query($sql);
             if ($query->num_rows() > 0){
                    $result = array();
                    foreach($query->result_array() as $row){
                            $result[] = $row;
                    }
                    return $result;
             }
             
        }

        //fungsi-fungsi yang berhubungan dengan pengelolaan pengguna
        public function getAllPengguna()
        {
            $query = $this->db->get('pengguna');
            return $query->result();
        }

        public function tambahPengguna($user_id,$name,$password,$email,$gender,$address,$telephone)
        {
            $sql = "insert into pengguna (userID,name,password,email,gender,address,telephone) values ('$user_id','$name','$password','$email','$gender','$address','$telephone')";
            $this->db->query($sql);
            $sql1 = "insert into role (roleName, userID) values ('Mahasiswa','$user_id')";
            $this->db->query($sql1);
        }

        public function getDataPengguna($user_id)
        {
            $sql = "select * from pengguna where userID = '$user_id'";
            $query = $this->db->query($sql);
            /*if ($query->num_rows() > 0) {
                $result = array();
                foreach ($query->result_array() as $row) {
                    $result[] = $row;
                }
                return $result;
            }*/
            return $query->row();
        }

        public function updatePengguna($user_id,$name,$password,$email,$gender,$address,$telephone)
        {
            $sql = "update pengguna SET name='$name',password='$password',email='$email',gender='$gender',address='$address',telephone='$telephone' WHERE userID = '$user_id'";
            $this->db->query($sql);
        }

        public function deletePengguna($user_id)
        {
            $sql = "delete from pengguna where userID='$user_id'";
            $this->db->query($sql);
        }

        function getByUsername($username) {
            $sql = 'select pengguna.*, role.roleName from pengguna left join role on pengguna.userID = role.userID where pengguna.userID = ?';
            $query = $this->db->query($sql, array($username));
            return $query->row_array();
        }

        function getRoleName($username){
            $sql = 'select roleName from role where userID = ?';
            $query = $this->db->query($sql, array($username));
            $roles = array();
            foreach($query->result_array() as $row){
                $roles[] = $row['roleName'];
            }
            return $roles;
        }

}

?>
